var form, table, tableInstance, iframeIndex;

$(function () {
    initLayUI(function () {
        initButtons();
    })
});

function initLayUI(cb) {
    layui.use(['table', 'form'], function () {
        table = layui.table;
        form = layui.form;

        form.render();

        tableInstance = table.render({
            elem: '#userTable',
            limits: [10, 15, 20],
            url: ServerURL + "/user/list",
            headers: tokenUtil.getAuthHeader(),
            method: "POST",
            toolbar: '#toolbar',
            cols: [[
                {field: 'userId', title: '用户编号', align: 'center', minWidth: 50},
                {field: 'loginAccount', title: '账号', align: 'center', minWidth: 100},
                {field: 'userName', title: '用户昵称', align: 'center', minWidth: 100},
                {field: 'phone', title: '手机号码', align: 'center', minWidth: 100},
                {
                    field: 'isEnabled', title: '状态', align: 'center', minWidth: 50, templet: function (d) {
                        if (d.isEnabled == 1) {
                            return "<input type='checkbox' value='" + d.isEnabled + "' data-userId='" + d.userId + "'" +
                                " lay-filter='isEnabled' checked='checked' lay-skin='switch' lay-text='启用|禁用' >";
                        } else {
                            return "<input type='checkbox' value='" + d.isEnabled + "'  data-userId='" + d.userId + "'" +
                                " lay-filter='isEnabled'  lay-skin='switch' lay-text='启用|禁用' >";
                        }
                    }
                },
                {field: 'createTime', title: '创建时间', align: 'center', minWidth: 100},
                {field: 'oper', title: '操作', minWidth: 300, align: 'center', toolbar: "#operBar"}
            ]],
            contentType: 'application/json',
            response: {
                statusName: 'code',
                statusCode: 200,
                msgName: 'msg',
                countName: 'count',
                dataName: 'data'
            },
            loading: true,
            page: true,
            error: function (res) {
                ajaxUtil.handleError(res);
            }
        });

        table.on('tool(userTable)', function (obj) {
            if (obj.event === 'delete') {
                deleteUser(obj);
            } else if (obj.event === 'update') {
                updateUser(obj);
            } else {
                resetPwdUser(obj);
            }
        });

        table.on('toolbar(userTable)', function (obj) {
            if (obj.event === 'add') {
                addUser();
            } else if (obj.event === 'export') {
                exportUser();
            } else {
                importUser();
            }
        });

        form.on('switch(isEnabled)', function (obj) {
            let userId = $(this).attr("data-userId");
            let isEnabled = obj.elem.checked;
            let msg = "禁用";
            if (isEnabled) {
                msg = "启用";
            }
            layerUtil.confirm("确定要【" + msg + "】该用户么?", function () {
                ajaxUtil.post({
                    "userId": userId,
                    "isEnabled": isEnabled
                }, "/user/changeEnabled", function (res) {
                    if (res.code === 200) {
                        layerUtil.success("操作成功");
                    } else {
                        layerUtil.error(res.msg);
                        obj.elem.checked = !obj.elem.checked;
                        form.render();
                    }
                });
            }, function () {
                obj.elem.checked = !obj.elem.checked;
                form.render();
            });
        });

        cb();
    });
}

function initButtons() {
    $("#searchBtn").click(function () {
        reloadTable();
    });

    $("#resetBtn").click(function () {
        form.val('form', {
            "loginAccount": "",
            "phone": "",
            "isEnabled": ""
        });
        reloadTable();
    });
}

function reloadTable() {
    let data = form.val('form');
    tableInstance.reload({
        where: data,
        page: {
            curr: 1
        }
    });
}

function deleteUser(obj) {
    layerUtil.confirm("确定要删除该用户么?", function () {
        ajaxUtil.simplePost({
            "userId": obj.data.userId
        }, "/user/delete", function () {
            reloadTable();
        });
    });
}

function addUser() {
    putTempData('updateUserId');
    iframeIndex = layerUtil.iframe({
        title: "新增",
        area: ['620px', '550px'],
        content: "view/auth/usersEdit.html"
    });
}

function updateUser(obj) {
    putTempData('updateUserId', obj.data.userId);
    iframeIndex = layerUtil.iframe({
        title: "修改",
        area: ['620px', '550px'],
        content: "view/auth/usersEdit.html"
    });
}

function resetPwdUser(obj) {
    putTempData('resetPwdUserId', obj.data.userId);
    iframeIndex = layerUtil.iframe({
        title: "重置密码",
        area: ['400px', '250px'],
        content: "view/auth/usersResetPwd.html"
    });
}

function exportUser() {
    console.info("exportUser");
}

function importUser() {
    console.info("importUser");
}